package library;

import java.io.*;
import java.util.*;

class Employee {
    String name;
    String id;

    public Employee(String name, String id) {
        this.name = name;
        this.id = id;
    }

    public double calculateSalary() {
        return 0;
    }

    public String getPaySlip() {
        return "员工: " + name + "(" + id + ") 工资: " + calculateSalary() + "元";
    }
}

class Worker extends Employee {
    double workHours;
    double hourRate;

    public Worker(String name, String id, double hourRate) {
        super(name, id);
        this.hourRate = hourRate;
    }

    public void setWorkHours(double hours) {
        this.workHours = hours;
    }

    @Override
    public double calculateSalary() {
        if (workHours < 0) {
            throw new RuntimeException("错误：工作时间不能为负数");
        }
        return workHours * hourRate;
    }

    @Override
    public String getPaySlip() {
        double salary = calculateSalary();
        return String.format("普通员工 %s(%s) | 工作%.1f小时 | 时薪%.1f元 | 工资%.1f元",
                name, id, workHours, hourRate, salary);
    }
}

class Manager extends Employee {
    double baseSalary;
    double bonus;

    public Manager(String name, String id, double baseSalary) {
        super(name, id);
        this.baseSalary = baseSalary;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public double calculateSalary() {
        if (bonus < 0) {
            throw new RuntimeException("错误：奖金不能为负数");
        }

        return baseSalary + bonus;
    }

    @Override
    public String getPaySlip() {
        double salary = calculateSalary();
        return String.format("管理人员 %s(%s) | 月薪%.1f元 | 奖金%.1f元 | 工资%.1f元",
                name, id, baseSalary, bonus, salary);
    }
}

// 工资管理系统
class SalarySystem {
    ArrayList<Employee> employees = new ArrayList<>();

    // 添加员工
    public void addEmployee(Employee emp) {
        employees.add(emp);
        System.out.println("添加: " + emp.name);
    }

    // 计算所有人工资
    public void calculateAll() {
        System.out.println("\n=== 工资计算 ===");
        for (Employee emp : employees) {
            try {
                double salary = emp.calculateSalary();
                System.out.println(emp.name + " 工资: " + salary + "元");
            } catch (Exception e) {
                System.out.println(emp.name + " 计算失败: " + e.getMessage());
            }
        }
    }

    // 打印所有工资条
    public void printAllPaySlips() {
        System.out.println("\n=== 工资条 ===");
        for (Employee emp : employees) {
            try {
                System.out.println(emp.getPaySlip());
            } catch (Exception e) {
                System.out.println(emp.name + " 生成工资条失败: " + e.getMessage());
            }
        }
    }

    // 保存到文件
    public void saveToFile() {
        try {
            PrintWriter writer = new PrintWriter("salary.txt");
            for (Employee emp : employees) {
                try {
                    writer.println(emp.getPaySlip());
                } catch (Exception e) {
                    writer.println(emp.name + " 数据错误");
                }
            }
            writer.close();
            System.out.println("\n工资数据已保存到 salary.txt");
        } catch (Exception e) {
            System.out.println("保存文件失败");
        }
    }

    public void showEmployees() {
        System.out.println("\n=== 员工列表 ===");
        for (Employee emp : employees) {
            String type = (emp instanceof Worker) ? "普通员工" : "管理人员";
            System.out.println(type + " - " + emp.name + "(" + emp.id + ")");
        }
    }
}

public class SimpleSalarySystem {
    public static void main(String[] args) {
        SalarySystem system = new SalarySystem();

        Worker worker1 = new Worker("张三", "001", 25.0);
        worker1.setWorkHours(160);

        Worker worker2 = new Worker("李四", "002", 30.0);
        worker2.setWorkHours(180);

        Manager manager1 = new Manager("王总", "101", 8000.0);
        manager1.setBonus(2000.0);

        Manager manager2 = new Manager("赵经理", "102", 6000.0);
        manager2.setBonus(1500.0);

        system.addEmployee(worker1);
        system.addEmployee(worker2);
        system.addEmployee(manager1);
        system.addEmployee(manager2);

        system.showEmployees();

        system.calculateAll();

        system.printAllPaySlips();

        system.saveToFile();

        System.out.println("\n=== 测试异常 ===");
        Worker badWorker = new Worker("测试", "999", 20.0);
        badWorker.setWorkHours(-10);
        try {
            badWorker.calculateSalary();
        } catch (Exception e) {
            System.out.println("捕获异常: " + e.getMessage());
        }
    }
}